﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Threading;

namespace SEO.Spider
{
    /// <summary>
    /// 版权所有: 版权所有(C) 2011，Adin Lee
    /// 内容摘要: 抓取百度推广用户信息
    /// 完成日期：2011年6月14日
    /// 版    本：V1.0 
    /// 作    者：Adin
    /// </summary>
    public class AdBaidu
    {
        SEO.Utility.IniFile iniFile = new Utility.IniFile(System.Windows.Forms.Application.StartupPath + "\\config.ini");
        SEO.DAL.InformationDAL indal = new DAL.InformationDAL();
        delegate void Delegate();

        /// <summary>
        /// 抓取百度搜索结果
        /// </summary>
        /// <param name="strWebUrl">网址</param>
        /// <param name="strStartMarkString">开始标记</param>
        /// <param name="strEndMarkString">结束标记</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strKeywordsName">关键词</param>
        /// <param name="strSEType">搜索引擎类型</param>
        public void AccordingURLGetResultList(string strWebUrl, string strStartMarkString, string strEndMarkString, System.Windows.Forms.RichTextBox rb, string strKeywordsName, string strSEType)
        {
            try
            {
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding("gb2312");
                wc.OpenRead(strWebUrl);
                string strGetHtmlCode = wc.RespHtml;
                strGetHtmlCode = strGetHtmlCode.Replace("id=\"1\"", "{end}id=\"1\"");
                strGetHtmlCode = strGetHtmlCode.Replace("<table id=", "{start}<table id=");

                //匹配列表源代码
                Regex rx = new Regex(strStartMarkString + "(?<content>[\\s\\S]*)?" + strEndMarkString, RegexOptions.Singleline);
                string strGetListHtmlContent = rx.Match(strGetHtmlCode).Result("${content}");

                GetSearchResultsByLinks(strGetListHtmlContent, rb, strKeywordsName, strSEType);
            }
            catch (Exception ex)
            {
                string strError = "百度搜索结果列表抓取匹配失败，关键词：" + strKeywordsName + "\r\n" + ex.Message;
                SEO.Utility.Log.SaveErrorLog("抓取百度搜索结果入口 -> " + strError);
            }

        }

        /// <summary>
        /// 分析关键词的长尾关键词
        /// </summary>
        /// <param name="strGetHtmlContent">匹配到的HTML代码</param>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strKeywordsName">关键词</param>
        /// <param name="strSEType">搜索引擎类型</param>
        public void GetSearchResultsByLinks(string strGetHtmlContent, System.Windows.Forms.RichTextBox rb, string strKeywordsName, string strSEType)
        {
            try
            {
                //匹配右侧推广信息
                Regex rxRight = new Regex("推广链接(?<content>[\\s\\S]*)?<div class=\"r ec_bdtg\">", RegexOptions.Singleline);
                string strGetRightContent = rxRight.Match(strGetHtmlContent).Result("${content}");
                strGetRightContent = strGetRightContent.Replace("</div><br>", "$");//分割每一个企业推广链接
                strGetRightContent = strGetRightContent.Replace("<br>", "|");//分割标题及内容
                strGetRightContent = SEO.Utility.StringHelper.CleanHtml(strGetRightContent);
                strGetRightContent = strGetRightContent.Replace("\r", "");
                strGetRightContent = strGetRightContent.Replace("\n", "");
                strGetRightContent = strGetRightContent.Replace("\t", "");
                strGetRightContent = strGetRightContent.Replace(".r.ec_bdtg{ width:238px;}.ec_bdtg .fsblock{padding:0;word-break:normal;font-family:arial}.ec_bdtg .fsblock a{text-decoration:none;}.ec_bdtg .title a{ text-decoration:underline; margin:0; padding:0; cursor:pointer;}", "");
                strGetRightContent = strGetRightContent.Substring(0, strGetRightContent.Length - 1);

                //飞利浦台灯,高质高效|飞利浦照明灯,最新LED技术,更加节能,更长寿命,更省钱,更柔和,营造别样浪漫氛围.|www.philips.com.cn/mylife$买台灯 耀点100全场促销 |台灯3折起,假1赔10/货到付款/全国配送| 免费退换,全场免运费.满199减50!|YaoDian100.com$三亚步行街水晶系列珠宝一条..|三亚步行街全新打造的海南旅游水晶系列珠宝一条街,是海南仅有的水晶系列一条街!|www.sybxj.com$台灯首选米兰梦工场 TEL:076..|台灯批发首选米兰梦工场专业灯饰厂家,质量绝对保证,以最大优惠回报给您!|www.ml-house.cn$宸宇涵艺术台灯供应商 0755-..|这不仅仅是家具,而是家具艺术,深圳宸宇涵文化发展有限公司集各种威尼斯镜,异型镜,挂镜|www.chenyuhanculture.com$房子装修了,该怎样选灯?|灯具的色彩,造型,式样,必须与室内装修和家具的风格相称,彼此呼应.华而不实的...|www.kvidi.com
                GetLineData(rb, strGetRightContent, strKeywordsName, "", "RightAd", strSEType);


                //匹配左侧推广信息
                Regex rxLeft = new Regex("{start}(?<content>[\\s\\S]*)?{end}", RegexOptions.Singleline);
                string strGetLeftContent = rxLeft.Match(strGetHtmlContent).Result("${content}");
                //推广链接
                strGetLeftContent = strGetLeftContent.Replace("<font color=\"#666666\">推广链接</font></a>","{TGLJ}");
                strGetLeftContent = strGetLeftContent.Replace("</table>", "$");
                strGetLeftContent = strGetLeftContent.Replace("<br>", "|");
                strGetLeftContent = strGetLeftContent.Replace("</a>", "|");
                strGetLeftContent = strGetLeftContent.Replace("<font size=\"-1\" color=\"#008000\" style=\"margin-left:6px;\">","|");
                strGetLeftContent = SEO.Utility.StringHelper.CleanHtml(strGetLeftContent);
                strGetLeftContent = strGetLeftContent.Replace("\r", "");
                strGetLeftContent = strGetLeftContent.Replace("\n", "");
                strGetLeftContent = strGetLeftContent.Replace("\t", "");
                strGetLeftContent = strGetLeftContent.Replace("<table cellpadding=\"0\" cellspacing=\"0\" class=\"result\"","");
                strGetLeftContent = strGetLeftContent.Replace("<table class=\"result-op\"  cellpadding=\"0\" cellspacing=\"0\"", "");
                strGetLeftContent = strGetLeftContent.Replace("{start}", "").Trim();
                strGetLeftContent = strGetLeftContent.Replace("&nbsp;", "");

                string strType = "";
                if (!strGetLeftContent.Contains("{TGLJ}"))
                {
                    //推广
                    strGetLeftContent = strGetLeftContent.Replace("||", "|");
                    strGetLeftContent = strGetLeftContent.Replace("推广|$|", "$").Trim();
                    //您想知道网上购物? 欢迎来到..|更多网上购物资讯尽在M18.Com,麦考林年度巨献宝马免费送,只需报名,0元领宝马!|www.M18.com
                    strType = "推广";
                }
                else
                {
                    strType = "推广链接";
                    //推广链接
                    //琅越陶瓷台灯批发 创新设计新理念|www.jdzyy.com|琅越陶瓷灯饰产品众多,涵养家具灯,宾馆客房灯,陶瓷台灯批发,壁灯,品种繁多,|
                }
                strGetLeftContent = strGetLeftContent.Replace("{TGLJ}", "");
                strGetLeftContent = strGetLeftContent.Substring(0, strGetLeftContent.Length - 1);
                GetLineData(rb, strGetLeftContent, strKeywordsName, strType, "LeftAd", strSEType);
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("获取网页地址列表 ->" + ex.Message);
            }
        }


        /// <summary>
        /// 获取每一行数据
        /// </summary>
        /// <param name="rb">显示实时消息控件</param>
        /// <param name="strGetHtmlContent">匹配到HTML代码</param>
        /// <param name="strKeywordsName">关键词</param>
        /// <param name="strType">广告类型：推广链接或推广</param>
        /// <param name="strPosition">广告位置：左侧或右侧</param>
        /// <param name="strSEType">搜索引擎类型</param>
        public void GetLineData(System.Windows.Forms.RichTextBox rb,string strGetHtmlContent, string strKeywordsName,string strType,string strPosition,string strSEType)
        {
            string strTitle = "";
            string strContent = "";
            string strUrl = "";
            string strPositionType = "";
            string strStatus = "";

            string[] splitContent = strGetHtmlContent.Split(new char[] { '$' });
            for (int i = 0; i < splitContent.Length; i++)
            {
                string strLineData = splitContent[i];

                SEO.Utility.PublicValue.SaveMessage(Convert.ToDateTime(DateTime.Now).ToString("MM-dd HH:mm:ss") + " 正在分析数据【" + strLineData + "】");
                rb.Text = SEO.Utility.PublicValue.GetMessage();

                try
                {
                    #region 右侧广告
                    if (strPosition == "RightAd")
                    {
                        strPositionType = "右侧广告";
                        try
                        {
                            //如果是右侧广告，有两种情况：一是广告创意只有一行数据，二是广告创意分两行数据
                            //例如：飞利浦台灯,高质高效|飞利浦照明灯,最新LED技术,更加节能,更长寿命,更省钱,更柔和,营造别样浪漫氛围.|www.philips.com.cn/mylife
                            //      $买台灯 耀点100全场促销 |台灯3折起,假1赔10/货到付款/全国配送| 免费退换,全场免运费.满199减50!|YaoDian100.com
                            //      $台灯首选米兰梦工场 TEL:076..|台灯批发首选米兰梦工场专业灯饰厂家,质量绝对保证,以最大优惠回报给您!|www.ml-house.cn
                            //      $宸宇涵艺术台灯供应商 0755-..|这不仅仅是家具,而是家具艺术,深圳宸宇涵文化发展有限公司集各种威尼斯镜,异型镜,挂镜|www.chenyuhanculture.com
                            //      $房子装修了,该怎样选灯?|灯具的色彩,造型,式样,必须与室内装修和家具的风格相称,彼此呼应.华而不实的...|www.kvidi.com

                            string[] splitRightAd = strLineData.Split(new char[] { '|' });
                            if (splitRightAd.Length > 3)
                            {
                                //创意分两行
                                strTitle = splitRightAd[0];
                                strContent = splitRightAd[1] + splitRightAd[2];
                                strUrl = splitRightAd[3];
                            }
                            else
                            {
                                //创意一行
                                strTitle = splitRightAd[0];
                                strContent = splitRightAd[1];
                                strUrl = splitRightAd[2];
                            }

                            if (strUrl.Contains("/"))
                            {
                                string[] splitUrl = strUrl.Split(new char[] { '/' });
                                strUrl = splitUrl[0];
                            }
                        }
                        catch (Exception ex)
                        {
                            strStatus = "匹配右侧推广链接内容失败！";
                            SEO.Utility.Log.SaveErrorLog("匹配右侧推广链接内容失败 -> " + ex.Message);
                        }
                    }
                    #endregion

                    #region 左侧广告
                    if (strPosition == "LeftAd")
                    {
                        strPositionType = "左侧广告";
                        //如果是左侧广告，有两种情况：一是推广链接，一是推广
                        if (strType == "推广链接")
                        {
                            try
                            {
                                //例如：搜房家天下台灯 3折起 限时抢购 疯狂促销 别犹豫!|shop.jiatx.com/|家天下台灯 限时抢购台灯 吸顶灯,吊灯,壁灯,落地灯,吊灯,水晶吊灯等台灯饰品任你选.|
                                string[] splitLeftAd = strLineData.Split(new char[] { '|' });
                                strTitle = splitLeftAd[0];
                                strContent = splitLeftAd[2];
                                strUrl = splitLeftAd[1];
                            }
                            catch (Exception ex)
                            {
                                strStatus = "匹配左侧广告推广链接内容失败！";
                                SEO.Utility.Log.SaveErrorLog("匹配左侧广告推广链接内容失败 -> " + ex.Message);
                            }
                        }
                        if (strType == "推广")
                        {
                            try
                            {
                                //例如：400电话申请 中国400电话网 400-888-9797|《中国400电话网》是中国最专业,规范的400电话受理网站,提供完善的400电话咨询,受理及售后服务.特别推荐联通400-600/666段,全国受理热线:400-888-9797,欢迎您的垂询!|联通400电话|400电话资费|申请400电话|400电话功能|www.400cn.cn 2011-06 - 
                                //      $400电话,400电话全国免费接听,免费开通!|400电话办理,400电话免选号费,精选号码!国内大型400电话办理机构,400电话办理 400-889-2788|www.fanlongcn.com 2011-06 - 
                                //      $全国400电话申请中心|专业,规范,开通快速,售后服务完善!提供电信400电话,网通400电话!|www.bainao.com 2011-06 - $
                                string[] splitLeftAd = strLineData.Split(new char[] { '|' });
                                if (splitLeftAd.Length > 4)
                                {
                                    strTitle = splitLeftAd[0];
                                    strContent = splitLeftAd[1];
                                    strUrl = splitLeftAd[6];
                                }
                                else
                                {
                                    strTitle = splitLeftAd[0];
                                    strContent = splitLeftAd[1];
                                    strUrl = splitLeftAd[2];
                                }

                                //匹配精准网址；www.bainao.com 2011-06 - 
                                strUrl = strUrl.Replace(" ", ",");
                                string[] splitUrl = strUrl.Split(new char[] { ',' });
                                strUrl = splitUrl[0];
                                if (strUrl.Contains("/"))
                                {
                                    string[] splitUrl2 = strUrl.Split(new char[] { '/' });
                                    strUrl = splitUrl2[0];
                                }
                            }
                            catch (Exception ex)
                            {
                                strStatus = "匹配左侧广告推广内容失败！";
                                SEO.Utility.Log.SaveErrorLog("匹配左侧广告推广内容失败 -> " + ex.Message);
                            }
                        }
                    }
                    #endregion

                    //加入数据库
                    strUrl = strUrl.ToLower();
                    strStatus = GetInsertData(strTitle, strContent, strUrl, strPositionType, strKeywordsName, strSEType);

                    SEO.Utility.PublicValue.SaveMessage("\r\n" + Convert.ToDateTime(DateTime.Now).ToString("mm:ss") + " 状态：" + strStatus);
                    rb.Text = SEO.Utility.PublicValue.GetMessage();

                }
                catch (Exception ex)
                {
                    SEO.Utility.Log.SaveErrorLog("获取每一行数据 -> " + ex.Message);
                }

                
            }
        }

        /// <summary>
        /// 获取数据准备添加到数据库中
        /// </summary>
        /// <param name="strTitle">广告创意标题</param>
        /// <param name="strContent">广告创意内容</param>
        /// <param name="strUrl">网站</param>
        /// <param name="strPosition">广告位置</param>
        /// <param name="strKeywordsName">关键词</param>
        /// <returns>返回状态</returns>
        public string GetInsertData(string strTitle, string strContent, string strUrl,string strPosition, string strKeywordsName,string strSEType)
        {
            string strStatus = "";
            
            if(string.IsNullOrEmpty(strTitle))
            {
                strStatus = "广告内容为空！";
            }
            else
            {
                //匹配广告标题中是否有号码存在
                string strPhones = SEO.Utility.StringHelper.GetPhones(strTitle);
                if (string.IsNullOrEmpty(strPhones))
                {
                    //如果标题中没有，则匹配内容中
                    strPhones = SEO.Utility.StringHelper.GetPhones(strContent);
                }

                try
                {
                    //判断是否勾选了长尾词必须包含关键词
                    string strContain = iniFile.GetString("OPTION", "contain", "");
                    if (strContain == "1")
                    {
                        if (strTitle.Contains(strKeywordsName.Trim()))
                        {
                            strStatus = InsertData(strKeywordsName, strTitle, strContain, strUrl, strPosition, strPhones, strSEType);
                        }
                    }
                    else
                    {
                        strStatus = InsertData(strKeywordsName, strTitle, strContain, strUrl, strPosition, strPhones, strSEType);
                    }
                }
                catch (Exception ex)
                {
                    strStatus = "添加失败！";
                    SEO.Utility.Log.SaveErrorLog("获取数据准备添加到数据库中 -> " + ex.Message);
                }
            }

            return strStatus;
        }

        /// <summary>
        /// 添加采集信息到数据库中
        /// </summary>
        /// <param name="strKeywords">关键词</param>
        /// <param name="strTitle">创意广告标题</param>
        /// <param name="strContent">创意广告内容</param>
        /// <param name="strUrl">网址</param>
        /// <param name="strPosition">位置</param>
        /// <param name="strPhones">电话号码</param>
        /// <param name="strSEType">搜索引擎类型</param>
        /// <returns>返回状态</returns>
        public string InsertData(string strKeywords,string strTitle,string strContent,string strUrl,string strPosition,string strPhones,string strSEType)
        {
            string strReturnValue = "";
            try
            {
                string strCompanyName = "";
                string strCompanyPhone = "";
                string strCompanyEmail = "";
                string strContactUs = "";

                //判断是否勾选采集竞价信息
                string strJingjia = iniFile.GetString("OPTION", "jingjia", "");
                if (strJingjia == "0")
                {
                    //不采集网站数据
                }
                else
                {
                    iniFile.WriteValue("OPTION", "weburl", strUrl);
                    //采集网站数据
                    bool bValue = false;
                    bValue = CallTime(GetWebsiteInfo, TimeSpan.FromMilliseconds(3000), null);//设定超时时间为5秒

                    if (!bValue)
                    {
                        strReturnValue = "采集网站数据：超时,跳过";
                    }
                    else
                    {
                        string strGetInfo = iniFile.GetString("INFO", "webinfo", "");
                        string[] splitGetInfo = strGetInfo.Split(new char[] { '|' });
                        strCompanyName = splitGetInfo[0];
                        strCompanyPhone = splitGetInfo[1];
                        strCompanyEmail = splitGetInfo[2];
                        strContactUs = splitGetInfo[3];

                        strReturnValue = "采集网站数据：成功";
                    }
                }

                if (string.IsNullOrEmpty(strPhones.Trim()))
                {
                    if (strJingjia == "0")
                    {
                        //不采集网站数据
                        strReturnValue = "采集数据为空";
                    }
                    else
                    {
                        strPhones = strCompanyPhone;//把采集到公司电话赋值给推广电话
                        SEO.Model.Information imn = new Model.Information();
                        imn.SEID = indal.GetAutoAddNum();
                        imn.KeyName = strKeywords;
                        imn.SEType = strSEType;
                        imn.InfoTitle = strTitle;
                        imn.InfoContent = strContent;
                        imn.WebUrl = strUrl;
                        imn.Position = strPosition;
                        imn.PhoneResult = strPhones;
                        imn.CompanyName = strCompanyName;
                        imn.CompanyPhone = strCompanyPhone;
                        imn.CompanyEmail = strCompanyEmail;
                        imn.ContactUs = strContactUs;
                        SEO.BLL.InformationBLL inbll = new BLL.InformationBLL();
                        inbll.InsertInformationBLL(imn);

                        strPhones = string.IsNullOrEmpty(strPhones) ? "空" : strPhones;
                        strReturnValue = "采集到的数据为：" + strPhones;
                    }
                }
                else
                {
                    if (indal.CheckInformationIsExist(strPhones.Trim()))
                    {
                        strReturnValue = "电话号码重复";
                    }
                    else if (SEO.Utility.StringHelper.IsChina(strPhones) && string.IsNullOrEmpty(strCompanyPhone))
                    {
                        strReturnValue = "采集不到号码";
                    }
                    else if (string.IsNullOrEmpty(strPhones) && string.IsNullOrEmpty(strCompanyPhone))
                    {
                        strReturnValue = "采集不到号码";
                    }
                    else
                    {
                        SEO.Model.Information imn = new Model.Information();
                        imn.SEID = indal.GetAutoAddNum();
                        imn.KeyName = strKeywords;
                        imn.SEType = strSEType;
                        imn.InfoTitle = strTitle;
                        imn.InfoContent = strContent;
                        imn.WebUrl = strUrl;
                        imn.Position = strPosition;
                        imn.PhoneResult = strPhones;
                        imn.CompanyName = strCompanyName;
                        imn.CompanyPhone = strCompanyPhone;
                        imn.CompanyEmail = strCompanyEmail;
                        imn.ContactUs = strContactUs;
                        SEO.BLL.InformationBLL inbll = new BLL.InformationBLL();
                        inbll.InsertInformationBLL(imn);

                        strReturnValue = "采集成功2：" + strPhones;
                    }
                }
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("添加采集信息到数据库中 -> " + ex.Message);
            }

            return strReturnValue;
        }

        /// <summary>
        /// 匹配推广公司网站页面信息
        /// </summary>
        /// <returns>返回公司信息</returns>
        public void GetWebsiteInfo()
        {
            string strReturnValue = "";

            try
            {
                string strUrl = iniFile.GetString("OPTION", "weburl", "");
                strUrl = "http://" + strUrl;
                string strEncode = "";
                string strCompanyName = "";
                string strPhone = "";
                string strEmail = "";
                SEO.Utility.WebClient wc = new SEO.Utility.WebClient();
                wc.Encoding = System.Text.Encoding.GetEncoding("gb2312");
                wc.OpenRead(strUrl);
                string strGetHtmlCode = wc.RespHtml;
                strGetHtmlCode = strGetHtmlCode.ToLower();
                strGetHtmlCode = strGetHtmlCode.Replace("copyright", "版权所有");
                string strHtmlContent = "";
                if (strGetHtmlCode.Length > 500)
                {
                    strHtmlContent = strGetHtmlCode.Substring(0, 500);
                }
                else
                {
                    strHtmlContent = strGetHtmlCode;
                }

                //转化各种联系我们的样式
                strGetHtmlCode = strGetHtmlCode.Replace("聯繫我們", "联系我们");
                strGetHtmlCode = strGetHtmlCode.Replace("contact", "联系我们");
                strGetHtmlCode = strGetHtmlCode.Replace("contactus", "联系我们");
                if (strHtmlContent.Contains("gb2312"))
                {
                    strEncode = "gb2312";
                    strHtmlContent = strGetHtmlCode;
                    Regex rxTitle = new Regex("<title>(?<content>[\\s\\S]*)?</title>", RegexOptions.Singleline);
                    strCompanyName = rxTitle.Match(strHtmlContent).Result("${content}");
                }
                else
                {
                    strEncode = "utf-8";
                    SEO.Utility.WebClient wc2 = new SEO.Utility.WebClient();
                    wc2.Encoding = System.Text.Encoding.GetEncoding("utf-8");
                    wc2.OpenRead(strUrl);
                    strGetHtmlCode = wc2.RespHtml;
                    strHtmlContent = strGetHtmlCode;
                    Regex rxTitle = new Regex("<title>(?<content>[\\s\\S]*)?</title>", RegexOptions.Singleline);
                    strCompanyName = rxTitle.Match(strHtmlContent).Result("${content}");
                }

                if (strCompanyName.Length > 50)
                {
                    strCompanyName = strCompanyName.Substring(0, 50) + "..";
                }

                if (strHtmlContent.Contains("电话") || strHtmlContent.Contains("手机"))
                {
                    //采集首页联系信息
                    Regex rx = new Regex("版权所有(?<content>[\\s\\S]*)?</body>", RegexOptions.Singleline);
                    strHtmlContent = rx.Match(strHtmlContent).Result("${content}");

                    strHtmlContent = SEO.Utility.StringHelper.CleanHtml(strHtmlContent);
                    strHtmlContent = strHtmlContent.Replace("\r", "");
                    strHtmlContent = strHtmlContent.Replace("\n", "");
                    strHtmlContent = strHtmlContent.Replace("\t", "");

                    //匹配电话号码
                    strPhone = SEO.Utility.StringHelper.GetPhones(strHtmlContent);
                    //匹配电子邮件
                    strEmail = Regex.Match(strHtmlContent, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").ToString();

                    if (string.IsNullOrEmpty(strPhone) && string.IsNullOrEmpty(strEmail))
                    {
                        //如果首页特殊情况下没有采集到信息，则继续采集联系我们页面
                        strReturnValue = GetInfoByContact(strCompanyName, strUrl, strGetHtmlCode, strEncode);
                    }
                    else
                    {
                        strReturnValue = strCompanyName.Trim() + "|" + strPhone.Trim() + "|" + strEmail.Trim() + "|" + strHtmlContent.Trim();
                    }
                }
                else
                {
                    //首页没有联系内容，则继续采集联系我们页面
                    strReturnValue = GetInfoByContact(strCompanyName, strUrl, strGetHtmlCode, strEncode);
                }
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("匹配推广公司网站页面信息 -> " + ex.Message);
            }

            iniFile.WriteValue("INFO", "webinfo", strReturnValue);
        }

        /// <summary>
        /// 采集联系我们信息
        /// </summary>
        /// <param name="strCompanyname">公司名称</param>
        /// <param name="strUrl">联系我们页面地址</param>
        /// <param name="strGetHtmlCode">字符串内容</param>
        /// <param name="strEncode">编码</param>
        /// <returns>返回采集信息</returns>
        public  string GetInfoByContact(string strCompanyname,string strUrl, string strGetHtmlCode, string strEncode)
        {
            string strReturnValue = "";

            try
            {
                strGetHtmlCode = strGetHtmlCode.ToLower();
                string strHtmlContent = Regex.Match(strGetHtmlCode, @"<a.*?href=(.*?).*?>联系我们</a>").ToString();
                string strLinkUrl = "";
                if (string.IsNullOrEmpty(strHtmlContent))
                {
                    //如果首页没有联系我们文字链接，则匹配图片链接
                    Regex reg = new Regex(@"<a[^<>]*?hrefs*=s*['""s]([^""']*)['""][^<>]*?>(.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    for (Match mh = reg.Match(strGetHtmlCode); mh.Success; mh = mh.NextMatch())
                    {
                        string strGetContactUrl = mh.Groups[1].ToString();
                        if (strGetContactUrl.Contains("contact") || strGetContactUrl.Contains("lxwm") || strGetContactUrl.Contains("lx") || strGetContactUrl.Contains("content"))
                        {
                            //contact/lx/lxwm
                            strLinkUrl = strGetContactUrl;
                            break;
                        }
                    }
                }
                else
                {
                    //匹配到了联系我们文字链接
                    strLinkUrl = SEO.Utility.StringHelper.GetHref(strHtmlContent);
                }

                if (!strLinkUrl.Contains("www"))
                {
                    strLinkUrl = strUrl + "/" + strLinkUrl;
                }


                //如果找到相关页面，则继续匹配
                SEO.Utility.WebClient wc3 = new SEO.Utility.WebClient();
                wc3.Encoding = System.Text.Encoding.GetEncoding(strEncode);
                wc3.OpenRead(strLinkUrl);
                string strGetContactContent = wc3.RespHtml;
                strGetContactContent = strGetContactContent.Replace("\r", "");
                strGetContactContent = strGetContactContent.Replace("\n", "");
                strGetContactContent = strGetContactContent.Replace("\t", "");
                strGetContactContent = SEO.Utility.StringHelper.CleanHtml(strGetContactContent);
                strGetContactContent = strGetContactContent.Replace(" ", "");
                strGetContactContent = strGetContactContent.Replace("　　", "");

                if (strGetContactContent.Contains("电话") || strGetContactContent.Contains("地址") || strGetContactContent.Contains("手机"))
                {
                    int iLength = strGetContactContent.Length;
                    int iStartPosition1 = strGetContactContent.IndexOf("电话");
                    int iStartPosition2 = strGetContactContent.IndexOf("地址");
                    int iStartPosition3 = strGetContactContent.IndexOf("手机");
                    int iStartPosition = 0;
                    if (iStartPosition1 > 0)
                    {
                        iStartPosition = iStartPosition1;
                    }
                    else if (iStartPosition2 > 0)
                    {
                        iStartPosition = iStartPosition2;
                    }
                    else
                    {
                        iStartPosition = iStartPosition3;
                    }

                    strGetContactContent = strGetContactContent.Substring(iStartPosition - 30, iLength - iStartPosition);

                    //匹配电话号码
                    string strPhone = SEO.Utility.StringHelper.GetPhones(strGetContactContent);
                    //匹配电子邮件
                    string strEmail = Regex.Match(strGetContactContent, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*").ToString();

                    strReturnValue = strCompanyname.Trim() + "|" + strPhone.Trim() + "|" + strEmail.Trim() + "|" + strGetContactContent.Trim();
                }
                else
                {
                    strReturnValue = strCompanyname.Trim() + "|||" + strGetContactContent.Trim();
                }
            }
            catch (Exception ex)
            {
                SEO.Utility.Log.SaveErrorLog("采集联系我们信息 -> " + ex.Message);
            }

            return strReturnValue;
        }

        /// <summary>
        /// 执行指定的方法,如果在指定的时间之内没有完成,则中止
        /// </summary>
        /// <param name="func">任务过程</param>
        /// <param name="timeSpan">超时时间</param>
        /// <param name="timeoutCallback">如果超时，则调用该方法</param>
        /// <returns>是否正确执行完毕</returns>
        private  bool CallTime(Delegate func, TimeSpan timeSpan, Delegate timeoutCallback)
        {
            if (func == null)
                throw new ArgumentNullException("func");

            ManualResetEvent resetEvent = new ManualResetEvent(false);
            ManualResetEvent waitThreadEvent = new ManualResetEvent(false);

            Exception error = null;
            Thread thread = null;

            // 将任务加到线程当中
            ThreadPool.QueueUserWorkItem(delegate
            {

                thread = Thread.CurrentThread;
                try { func(); }
                catch (ThreadAbortException) { }
                catch (Exception ex) { error = ex; }

                resetEvent.Set();
                waitThreadEvent.WaitOne();  // 每次线程执行结束都等待后续的处理逻辑
            });

            try
            {
                bool result = resetEvent.WaitOne(timeSpan, false);  // 等待任务的结束

                if (error != null)  // 说明在执行过程中出现异常，直接抛出异常
                    throw error;

                if (!result)
                {
                    if (thread != null)
                    {
                        thread.Abort();  // 此时可以确保该线程没有开始运行新的任务
                        waitThreadEvent.Set();
                    }

                    if (timeoutCallback != null)
                        timeoutCallback();
                }

                return result;
            }
            finally
            {
                waitThreadEvent.Set();  // 最后确保释放线程池线程
            }
        }
    }
}
